function[class] = kNNClassification(featureVec, k, collectedData)
% Classifies the given feature vector using k-NN on extracted features.
%
%   INPUT
%   featureVec......The feature vector that shall be classified
%   k...............the number of nearest neighbours regarded by the
%                   algorithm
%   collectedData...the 'knowledge' of the k-NN algorithm; an array in the
%                   same format as returned by loadWines().
%   OUTPUT
%   class...........the classification for the given vector as the id.

    % calculate the difference between the feature vector of the given image
    % and the feature vectors in the training dataset
    difference = collectedData(2:end, :) - (ones(size(collectedData, 2), 1) * featureVec')';
    % square the difference in every dimension and sum the results (our k-NN
    % uses the euclidean distance); no need for taking the square-root since
    % it would not affect the sorting order.
    distance = sum(difference .^ 2, 1);
    [notNeeded, indizes] = sort(distance);
    % indizes now contains the IDs of training data vectors in the order of
    % increasing distance from the given feature vector
    % choose the class that occurs mostly within the first (nearest) k
    % vectors and return it
    class = mode(collectedData(1, indizes(1:k)));
end
